package com.awa.usercenter.exception;

import com.awa.usercenter.common.BaseResponse;
import com.awa.usercenter.common.Code;
import com.awa.usercenter.common.utils.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@RestControllerAdvice   //Spring切面功能，在任意代码的前后封装，在执行代码前或其后做额外的处理
@Slf4j
public class GlobalExceptionHandler {

    // 该注解只接收继承了异常接口的类，通过一个注解来定义我要针对什么异常来做什么处理
    @ExceptionHandler(BusinessException.class)
    public BaseResponse businessExceptionHandler(BusinessException e){
        log.error("businessException" + e.getMessage(), e);
        return ResultUtils.error(e.getCode(), e.getMessage(), e.getDescription());
    }

    @ExceptionHandler(RuntimeException.class)
    public BaseResponse runtimeExceptionHandler(RuntimeException e){
        log.error("runtimeException", e);
        return ResultUtils.error(Code.SYSTEM_ERROR, e.getMessage(), "");
    }
}
